 # CSSmin.py for Notepad++ Python Scripting plugin
# https://github.com/ethanpil/npp-cssmin
# This is a simple script that contains a Python port of the YUI CSS Compressor so you can minify both CSS and JS
#
# Version 1.0
#
## Installation
#  Simply copy **CSSmin.py** to **\plugins\PythonScript\scripts\** in your NPP folder and restart NPP. It will now appear as a menu item under Plugins...Python Script...Scripts
#
## Usage
#  1. Select the text to process, or select nothing.
#  2. Go to the NPP menu, Plugins...Python Script...Scripts...CSSmin and click!
#  3. If selected text is detected, it wil process and replace the selected text, otherwise the entire contents of current document.
#  4. Undo is available if you dont like the results
#
##Credits
#  Original cssmin.py ported from YUI here https://github.com/zacharyvoase/cssmin 
###

from StringIO  import StringIO 
import re

def css_format(css):
	css = re.sub(r"(}|;})", r"\n\1\n", css)
	css = re.sub(r"({|;)", r"\1\n\t", css)
	return css


#Finished defining functions. Now execute.

editor.beginUndoAction()
seltext = editor.getSelText()
seltextlen = len(seltext)

if seltextlen >= 1 :
	rawtext = seltext
	mintext = css_format(rawtext)
	editor.replaceSel(mintext)
else:
	rawtext = editor.getText()
	mintext = css_format(rawtext)
	editor.setText(mintext)

editor.endUndoAction()